import time
from selenium import webdriver
from selenium.webdriver.support.select import Select
class shop_tuiguang:
    def __init__(self):
        dr=webdriver.Firefox()
        self.s="http://192.168.0.118"
        dr.implicitly_wait(3)
        dr.get(self.s + "/TinyShop//index.php?con=admin&act=login")
        self.dr=dr
    def h_login(self,name,pwd):##后台登录
        dr=self.dr
        dr.find_element_by_name("name").clear()
        dr.find_element_by_name("name").click()
        dr.find_element_by_name("name").send_keys(name)
        dr.find_element_by_name("password").clear()
        dr.find_element_by_name("password").click()
        dr.find_element_by_name("password").send_keys(pwd)
        dr.find_element_by_name("verifyCode").send_keys("aaaa")
        dr.find_element_by_xpath("//input[@value=\"登 录\"]").click()
    def shop_add(self,li):
        dr=self.dr
        dr.find_element_by_link_text("营销推广").click()
        dr.find_element_by_link_text("添加").click()
        dr.find_element_by_name("name").send_keys(li[0])##促销活动名称
        ele=dr.find_element_by_id("prom_type")##下拉框
        Select(ele).select_by_index(li[1])
        dr.find_element_by_name("expression").send_keys(li[2])##折扣
        dr.find_element_by_name("start_time").send_keys(li[3])
        dr.find_element_by_name("end_time").send_keys(li[4])
        dr.find_element_by_name("group[]").click()##勾选默认
        dr.find_element_by_xpath("/html/body/div[3]/div[2]/form/div/div[1]/div[1]/dl[7]/dd/a").click()
        time.sleep(2)
        dr.switch_to.frame(dr.find_element_by_tag_name("iframe"))##切换焦点
        time.sleep(1)
        dr.find_element_by_xpath("/html/body/div[2]/form/div[2]/table/tbody/tr[1]/td[1]/input").click()##选择全选
        dr.find_element_by_xpath("/html/body/div[3]/button").click()##选择添加
        dr.switch_to_default_content()
        dr.find_element_by_css_selector("input.button").click()
        return dr
    def shop_delete(self):
        dr=self.dr
        dr.find_element_by_link_text("营销推广").click()
        dr.find_element_by_xpath("(//input[@name='id[]'])[3]").click()
        dr.find_element_by_xpath("/html/body/div[3]/div[2]/form/table/tbody/tr[4]/td[2]/div/a").click()
        dr.find_element_by_link_text("删除").click()
        dr.find_element_by_css_selector("button..aui_state_highlight").click()
    def __del__(self):
        self.dr.quit()



# if __name__=="__main__":
#     b=shop_tuiguang()
#     b.h_login('admin','163415')
#     # b.shop_add('77','2','9','2018-09-27 11:50:41','2018-09-28 11:50:41')
#     b.shop_delete()
